/* -----------------------------------------------------------------------------
 * PROJECT    : net.lwf
 * FILE       : WordSoundAction.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Dec 17, 2008       Raghu Sarraff			       Created
 */
package lwf.web.struts.action.english;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lwf.factory.PropertyReader;
import lwf.handler.IProcessHandler;
import lwf.handler.english.WordHandler;
import lwf.handler.english.WordSoundsHandler;
import lwf.web.struts.action.form.english.WordSoundActionForm;

import net.lwf.web.constant.IPageTitle;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;


/**
 * Type the class description here
 *  
 */
public class WordSoundAction extends WordAction {
	
	public ActionForward home(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		WordSoundActionForm wsaform = (WordSoundActionForm) form;
		initWordSounds(wsaform);
		
		WordSoundsHandler handler = new WordSoundsHandler();
		wsaform.setWordSoundKeys(handler.generateWordSoundKeys());
		return mapping.findForward("home");
	} //home

	/* (non-Javadoc)
	 * @see worksheet.struts.action.WordAction#getHandler(javax.servlet.http.HttpServletRequest, org.apache.struts.action.ActionForm)
	 */
	public WordHandler getHandler(HttpServletRequest request, ActionForm form) {
		WordSoundActionForm wsaform = (WordSoundActionForm) form;
		WordSoundsHandler handler = new WordSoundsHandler(wsaform.getWordSoundKey());
		return handler;
	}

	/* (non-Javadoc)
	 * @see worksheet.struts.action.WordAction#checkGrade(javax.servlet.http.HttpServletRequest, org.apache.struts.action.ActionForm)
	 */
	public void checkGrade(HttpServletRequest request, ActionForm form) {
	}

	/* (non-Javadoc)
	 * @see worksheet.struts.action.WordAction#getDisplayRows(org.apache.struts.action.ActionForm)
	 */
	public int getDisplayRows(ActionForm form) {
		return 0;
	}
	
	private void initWordSounds(WordSoundActionForm wsaform) {
		Map mapWordSounds = PropertyReader.getInstance().getWordSounds();
		Iterator iter = mapWordSounds.entrySet().iterator();
		List lstStarting = new ArrayList();
		List lstEnding = new ArrayList();
		while(iter.hasNext()) {
			Map.Entry mapentry = (Map.Entry) iter.next();
			String key = (String) mapentry.getKey();
			StringTokenizer stk = new StringTokenizer(key, "|");
			String sound = stk.nextToken();
			String soundKey = stk.nextToken();

			if(soundKey.equals("1")) {
				//Starting
				lstStarting.add(sound);
			}
			else if(soundKey.equals("2")) {
				//Ending
				lstEnding.add(sound);
			}
		}
        wsaform.setStartingWS(lstStarting);
        wsaform.setEndingWS(lstEnding);
	}

	@Override
	public IProcessHandler getHandler(HttpServletRequest request) {
		return null;
	}

	@Override
	public String getPageTitle() {
		return IPageTitle.WORD_SOUNDS;
	}

	@Override
	public String getMainTitle() {
		return null;
	}

	@Override
	public String getPdfLink() {
		return null;
	}

	@Override
	public String getQuizLink() {
		return null;
	}

}

